﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HeadLinx.AD.Common;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;
using HeadLinx.AD.Explorer.Properties;

namespace HeadLinx.AD.Explorer
{
    /// <summary>
    /// Interaction logic for MainMenuWindow.xaml
    /// </summary>
    public partial class MainMenuWindow : Window
    {
        //private const string THUMBNAIL_PHOTO_CACHE_FOLDER = "HeadLinx Thumbnail Photos";

        public MainMenuWindow()
        {
            InitializeComponent();
        }

        private void UpdateRunningAsText()
        {
            txtRunningAs.Text = LocalUtility.GetCurrentUserAlias();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateRunningAsText();

            //txtRunningAs.ContextMenu = GetRunningAsContextMenu();

            //try
            //{
            //    App.PhotoCache = new HeadLinxPhotoCache(THUMBNAIL_PHOTO_CACHE_FOLDER);
            //}
            //catch (Exception ex)
            //{
            //    App.PhotoCache = null;
            //    MessageBox.Show("Thumbnail photos will not be cached due error creating cache folder:"
            //        + Environment.NewLine + ex.Message,
            //        "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
            //}
        }

        private void btnPeopleLinks_Click(object sender, RoutedEventArgs e)
        {
            ShowLinksDialog dlg = new ShowLinksDialog();
            dlg.ShowDialog();
        }

        private void btnMyPeers_Click(object sender, RoutedEventArgs e)
        {
            string self = LocalUtility.GetCurrentUserAlias();
            string title = "Peers of " + self;
            LocalUtility.RunQueryAndShowResults(this, HeadLinxQueryType.Peers, self, title, false);
        }

        private void btnMySuperiors_Click(object sender, RoutedEventArgs e)
        {
            string self = LocalUtility.GetCurrentUserAlias();
            string title = "Superiors of " + self;
            LocalUtility.RunQueryAndShowResults(this, HeadLinxQueryType.Superiors, self, title, false);
        }

        private void btnMyReports_Click(object sender, RoutedEventArgs e)
        {
            string self = LocalUtility.GetCurrentUserAlias();
            string title = "Direct reports of " + self;
            LocalUtility.RunQueryAndShowResults(this, HeadLinxQueryType.DirectReports, self, title, false);
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnMinimize_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void DoOpenChartFile()
        {
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.CheckFileExists = true;
                dlg.Multiselect = false;
                dlg.ShowReadOnly = false;
                dlg.Title = "Open HeadLinx Chart";
                dlg.AddExtension = true;
                dlg.Filter = "HeadLinx Chart (*.hlx)|*.hlx|All Files (*.*)|*.*";
                dlg.DefaultExt = "hlx";

                // Restore dir doesn't seem to restore last dir so we do it manually.
                //dlg.RestoreDirectory = true;

                LocalUtility.LoadInitialDialogFolder(dlg);

                bool? ok = dlg.ShowDialog();

                if (ok.Value == true)
                {
                    LocalUtility.SaveInitialDialogFolder(dlg);

                    this.Cursor = Cursors.Wait;

                    // Show dialog and return (block==false)
                    LocalUtility.LoadAndShowChartFile(dlg.FileName, false);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.Cursor = null;
            }
        }

        private void btnLoadFromFile_Click(object sender, RoutedEventArgs e)
        {
            DoOpenChartFile();
        }

        private void mnuBrowsePhotoCache_Click(object sender, RoutedEventArgs e)
        {
            if (App.PhotoCache != null)
            {
                Process.Start(App.PhotoCache.CacheFolder);
            }
        }

        private void mnuOpenFile_Click(object sender, RoutedEventArgs e)
        {
            DoOpenChartFile();
        }

        private void mnuAbout_Click(object sender, RoutedEventArgs e)
        {
            AboutDialog dlg = new AboutDialog();
            dlg.ShowDialog();
        }

        private void mnuSetRunningAs_Click(object sender, RoutedEventArgs e)
        {
            SetRunningAsDialog dlg = new SetRunningAsDialog();
            dlg.ShowDialog();
            UpdateRunningAsText();
        }

        private void mnuPhotoCacheSettings_Click(object sender, RoutedEventArgs e)
        {
            PhotoCacheSettingsDialog dlg = new PhotoCacheSettingsDialog();
            dlg.ShowDialog();
        }

        private void mnuResetSettings_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show(
                "All saved settings will be reset to defaults.  OK to continue?",
                "Reset Settings",
                MessageBoxButton.OKCancel,
                MessageBoxImage.Warning);

            if (result == MessageBoxResult.OK)
            {
                Settings.Default.Reset();
                Settings.Default.Save();
            }
        }

        private void mnuExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
